package com.example.s.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.example.s.R;
import com.example.s.application.MyApplication;
import com.example.s.network.MyNetwork;
import com.example.s.utils.DeviceUtil;
import com.example.s.utils.PermissionsUtils;

import java.util.HashMap;

public class BaseActivity extends AppCompatActivity implements ViewTreeObserver.OnGlobalLayoutListener {

    public String TAG;
    private FrameLayout content;
    private boolean mLayoutComplete = false;
    private OnDeviceKeyboardListener onDeviceKeyboardListener;
    public MyNetwork N;
    public BaseActivity A;

    public HashMap<String, Object> getMap() {
        return new HashMap<String, Object>();
    }


    public SharedPreferences getSharedPreferences(String name) {
        return super.getSharedPreferences(name, MODE_PRIVATE);
    }


    public void startActivity(Class<?> clazz) {
        super.startActivity(new Intent(this,clazz));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        super.onCreate(savedInstanceState);
        A = this;
        MyApplication.getInstance(A);
        content = (FrameLayout) findViewById(android.R.id.content);
        content.post(new Runnable() {
            @Override
            public void run() {
                mLayoutComplete = true;
                Log.e(TAG, "content 布局完成");
            }
        });
        content.getViewTreeObserver().addOnGlobalLayoutListener(this);
        TAG = getClass().getSimpleName();
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        overridePendingTransition(R.anim.slide_right_in, R.anim.slide_left_out);//设置activity进入和退出动画


        //初始化
        N = MyNetwork.getInstance(this);
    }


    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.slide_back_right_out);
    }

    @Override
    public void onGlobalLayout() {
        if (!mLayoutComplete)
            return;
        boolean navigationBarShown = DeviceUtil.getInstance().isNavigationBarShow(this);
        int navigationBarHeight = DeviceUtil.getInstance().getNavigationBarHeight(this);
        if (navigationBarShown) {
            if (onDeviceKeyboardListener != null) {
                Log.d(TAG, "onGlobalLayout:虚拟按键显示 ");
                int currentNavigationBarHeight = DeviceUtil.getInstance().getCurrentNavigationBarHeight(this);
                Log.d(TAG, "onGlobalLayout: " + currentNavigationBarHeight);
                onDeviceKeyboardListener.onShow(navigationBarHeight, currentNavigationBarHeight);
            }
        } else {
            if (onDeviceKeyboardListener != null) {
                Log.d(TAG, "onGlobalLayout:虚拟按键隐藏 ");
                onDeviceKeyboardListener.onHide(navigationBarHeight);
            }

        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        content.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    }

    public void setOnDeviceKeyboardListener(OnDeviceKeyboardListener onDeviceKeyboardListener) {
        this.onDeviceKeyboardListener = onDeviceKeyboardListener;
    }

    public interface OnDeviceKeyboardListener {
        void onShow(int NavigationBarHeight, int currentNavigationBarHeight);

        void onHide(int NavigationBarHeight);
    }
}
